package zhaotest;

import com.rljk.common.consts.AssetConst;
import com.rljk.utils.SignUtil;
import com.tencent.trustsql.sdk.ErrorNum;
import com.tencent.trustsql.sdk.TrustSDK;
import com.tencent.trustsql.sdk.demo.command.Command;
import com.tencent.trustsql.sdk.exception.TrustSDKException;
import org.apache.commons.lang3.StringUtils;

import javax.crypto.NoSuchPaddingException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

public class SignStrCommand implements Command {

	@Override
	public String execute(String... args) throws Exception {
		if (args == null || args.length < 3 || StringUtils.isEmpty(args[1]) || StringUtils.isEmpty(args[2])) {
			throw new TrustSDKException(ErrorNum.INVALID_PARAM_ERROR.getRetCode(),
					ErrorNum.INVALID_PARAM_ERROR.getRetMsg());
		}
		String prvKey = args[1];
		String srcStr = args[2];
		try {
			System.out.println(String.format("Sign a string [%s] with private key [%s]: ", srcStr, prvKey));
			String sign = TrustSDK.signString(prvKey, srcStr.getBytes("UTF-8"), false);
			return sign;
		} catch (Exception e) {
			throw new TrustSDKException(ErrorNum.INVALID_PARAM_ERROR.getRetCode(),
					ErrorNum.INVALID_PARAM_ERROR.getRetMsg());
		}                  
	}

	public static void main(String[] args) throws UnsupportedEncodingException, TrustSDKException, NoSuchAlgorithmException, NoSuchPaddingException {
		String sign = TrustSDK.signString(AssetConst.COMMUNICATION_PRIVATE_KEY,"Tencent TrustSQL".getBytes("UTF-8"), false);
		System.out.println(sign);
	}

}
